/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Almacen;
import dp1.titandevelop.titano.persistent.Producto;
import dp1.titandevelop.titano.service.AlmacenService;
import dp1.titandevelop.titano.service.ConexionService;
import dp1.titandevelop.titano.service.DocumentoVentaService;
import dp1.titandevelop.titano.service.ProductoService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.view.ventas.reporte_venta;
import java.awt.Cursor;
import java.awt.FileDialog;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_ReporteMateriales extends javax.swing.JInternalFrame {

    private ArrayList<Producto> listaProd;
   
    private Mensajes sms = new Mensajes();
    
    private void addList (){
        
        for(int i=0;i<listaProd.size();i++){
            this.cmbProducto.addItem(listaProd.get(i).getDescripcion());
        }
    }
    
    public ModulosT_PRO_ReporteMateriales() {
        initComponents();
        
        //ProductoService s = new ProductoService();
        
        DocumentoVentaService s=new DocumentoVentaService();
        this.listaProd = (ArrayList)s.listProduct();
        addList();
    }
    public int cboxIndex(JComboBox cbox){
        return cbox.getSelectedIndex()-1;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporte = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmbProducto = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();

        setResizable(true);
        setTitle("Reporte de Materiales y Recetas");

        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/reporte2.png"))); // NOI18N
        btnReporte.setToolTipText("Click para generar reporte");
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Producto:");

        cmbProducto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));

        jLabel2.setText("Cantidad:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnReporte)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addGap(18, 18, 18))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnReporte)
                    .addComponent(btnCancelar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed
        
        ValidacionService val = new ValidacionService();
        
       {
            ValidacionService validacionService = new ValidacionService();
            
            try{
                
                if(validacionService.esCBoxVacio(cmbProducto) || validacionService.esVacio(txtCantidad)){
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"Debe llenar los filtros para generar el reporte", "Aviso", JOptionPane.WARNING_MESSAGE);
                }else{
                String nombreArchivo = "/tmp/reporte-materiales.pdf";
            FileDialog fd = new FileDialog(new JFrame(), "Guardar", FileDialog.SAVE);
            fd.setDirectory(".");
            fd.setFile("*.pdf");
            fd.setVisible(true);
            nombreArchivo = fd.getDirectory() + fd.getFile();
                
                    if(fd.getFile()!=null){
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    

                        int indexProductos =cboxIndex(cmbProducto), idProd;
                        idProd = this.listaProd.get(indexProductos).getIdproducto();
                        Float cant=Float.parseFloat(this.txtCantidad.getText());

                        reporte_receta print=new reporte_receta(idProd,cant,nombreArchivo);
                        this.setCursor(Cursor.getDefaultCursor());
                        JOptionPane.showInternalMessageDialog(this.getRootPane(),"El Reporte a sido generado satisfactoriamente","Mensaje",JOptionPane.INFORMATION_MESSAGE);

                    }
                }
                
                
                
                /*
                //Preparando el parámetros
                int indexProductos =cboxIndex(cmbProducto), idProd;
                //String ubicacionReporte=System.getProperty("user.dir");
                 File archivoReporte;
                Map parametro = new HashMap();

               {//Caso en que se elige solo el parametro almacen
                    idProd = this.listaProd.get(indexProductos).getIdproducto();//
                    archivoReporte = new File(getClass().getResource("/reportes/Reporte-Materiales.jasper").toURI());
                    parametro.put("idproducto",idProd);
                    parametro.put("cantidad",Float.parseFloat(this.txtCantidad.getText()));
                }
//                else{
//                    JOptionPane.showInternalMessageDialog(this.getRootPane(), "Debe ingresar el rango completo","Aviso",JOptionPane.WARNING_MESSAGE);
//                    return;
//                            
//                }
                  JasperReport jreport = (JasperReport) JRLoader.loadObject(archivoReporte);
                
                JasperPrint jasperPrint = JasperFillManager.fillReport(jreport,parametro,con);
                JasperViewer jviewer = new JasperViewer(jasperPrint,false);  
                jviewer.setVisible(true);*/  
                this.setCursor(Cursor.getDefaultCursor());
            }catch(Exception e){
             System.out.println("mensaje de Error:"+ e.getMessage());
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"Hubo un error en la generación del reporte","Error",JOptionPane.ERROR_MESSAGE);

            }
       
//        else{
//            JOptionPane.showInternalMessageDialog(this.getRootPane(), "Debe ingresar almenos el filtro o el rango completo","Aviso",JOptionPane.WARNING_MESSAGE);
//        }
    }//GEN-LAST:event_btnReporteActionPerformed
    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cmbProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtCantidad;
    // End of variables declaration//GEN-END:variables
}
